package com.test.project.system.pingfen.service.impl;

import java.util.List;

import com.test.common.exception.BusinessException;
import com.test.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.test.project.system.pingfen.mapper.TPingfenMapper;
import com.test.project.system.dajuan.domain.TDajuan;
import com.test.project.system.dajuan.service.ITDajuanService;
import com.test.project.system.pingfen.domain.TPingfen;
import com.test.project.system.pingfen.service.ITPingfenService;
import com.test.project.system.pingwei.domain.TPingwei;
import com.test.project.system.pingwei.service.ITPingweiService;
import com.test.common.utils.security.ShiroUtils;
import com.test.common.utils.text.Convert;

/**
 * 评委评分Service业务层处理
 * 
 * @author test
 * @date 2023-12-05
 */
@Service
public class TPingfenServiceImpl implements ITPingfenService 
{
    @Autowired
    private TPingfenMapper tPingfenMapper;
    
    @Autowired
    private ITPingweiService pingweiService;
    
    @Autowired
    private ITDajuanService dajuanService;

    /**
     * 查询评委评分
     * 
     * @param id 评委评分ID
     * @return 评委评分
     */
    @Override
    public TPingfen selectTPingfenById(Long id)
    {
        return tPingfenMapper.selectTPingfenById(id);
    }

    /**
     * 查询评委评分列表
     * 
     * @param tPingfen 评委评分
     * @return 评委评分
     */
    @Override
    public List<TPingfen> selectTPingfenList(TPingfen tPingfen)
    {
        return tPingfenMapper.selectTPingfenList(tPingfen);
    }

    /**
     * 新增评委评分
     * 
     * @param tPingfen 评委评分
     * @return 结果
     */
    @Override
    public int insertTPingfen(TPingfen tPingfen)
    {
        
        
        
    	TDajuan dajuan = dajuanService.selectTDajuanById(tPingfen.getPid());
    	TPingwei pingwei = new TPingwei();
    	pingwei.setPid(dajuan.getWenjuanId());
    	pingwei.setPingweiId(ShiroUtils.getUserId());
		List<TPingwei> selectTPingweiList = pingweiService.selectTPingweiList(pingwei );
		if(selectTPingweiList.size()>0) {
			tPingfen.setPingweiId(selectTPingweiList.get(0).getId());
		}else {
			throw new BusinessException("您不是评委！");
		}
        
        
        tPingfen.setCreateBy(ShiroUtils.getLoginName());
        
        TPingfen tPingfen1 = new TPingfen();
        tPingfen1.setPid(tPingfen.getPid());
        tPingfen1.setCreateBy(ShiroUtils.getLoginName());
        List<TPingfen> selectTPingfenList = selectTPingfenList(tPingfen1);
        if(selectTPingfenList.size()>0) {
        	throw new BusinessException("您已评分！");
        }

        tPingfen.setCreateTime(DateUtils.getNowDate());
        
        return tPingfenMapper.insertTPingfen(tPingfen);
    }

    /**
     * 修改评委评分
     * 
     * @param tPingfen 评委评分
     * @return 结果
     */
    @Override
    public int updateTPingfen(TPingfen tPingfen)
    {
        return tPingfenMapper.updateTPingfen(tPingfen);
    }

    /**
     * 删除评委评分对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTPingfenByIds(String ids)
    {
        return tPingfenMapper.deleteTPingfenByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除评委评分信息
     * 
     * @param id 评委评分ID
     * @return 结果
     */
    @Override
    public int deleteTPingfenById(Long id)
    {
        return tPingfenMapper.deleteTPingfenById(id);
    }
}
